package com.hhxy.producer;


import com.hhxy.utils.RabbitMqUtil;
import com.rabbitmq.client.AMQP;
import com.rabbitmq.client.Channel;

import java.util.HashMap;
import java.util.Map;

/**
 * @author ghp
 * @date 2023/2/27
 * @title
 * @description
 */
public class PriorityProducer {

    private static final String QUEUE_NAME = "priority.queue";

    public static void main(String[] args) throws Exception {

        Channel channel = RabbitMqUtil.getChannel();

        // 声明队列
        Map<String, Object> params = new HashMap<>();
        // 设置优先级队列最大标识（0~10）
        params.put("x-max-priority", 10);
        channel.queueDeclare(QUEUE_NAME, true, false, false, params);

        //给消息赋予一个priority属性
        AMQP.BasicProperties properties =
                new AMQP.BasicProperties().builder().priority(1).priority(10).build();

        for (int i = 0; i < 10; i++) {
            String message = "消息" + i;
            if (i == 5) {
                // 给第5条消息消息设置优先级
                channel.basicPublish("", QUEUE_NAME, properties, message.getBytes());
            } else {
                channel.basicPublish("", QUEUE_NAME, null, message.getBytes());
            }
            System.out.println("消息发送完成：" + message);
        }
    }
}
